﻿Wheels.NextTransport = new Ext.NextTransportList({ style: 'background-color: white' });

function getNextTransports() {
    var list = Wheels.NextTransport;

    this.isLoaded = false;
    Ext.getBody().unmask();
    Ext.getBody().mask(false, '<div class="demos-loading">Loading&hellip;</div>');

    var currentLocation = getGpsLocation();
    var radius = 0.05;
    var requestUrl = "http://transit.cloudapp.net/TransitODataService.svc/GeoAreas('" + currentLocation.Lat + "|" + currentLocation.Lng + "|" + radius + "')/Stops ?$expand=Arrivals&$select=StopId,Name,Point,Arrivals/ArrivalTime,Arrivals/RouteName,Arrivals/DirectionText,Arrivals/DirectionId,Arrivals/RouteType,Arrivals/RouteId";

    makeRequest(requestUrl, function (result) {
        if (result != null) {
            var stops = result.d.results;
            var arrivals = denormalize(stops); //.where(function (x) { return x.RouteId == '38B'; });
            var resultList = new Array();

            $.each(stops, function () {
                var stop = this;
                var nearestArrivals1 = arrivals.where(function (x) { return x.StopId == stop.Id && x.DirectionId == 0 }).sort(arrivalTimeSort).take(2);
                var nearestArrivals2 = arrivals.where(function (x) { return x.StopId == stop.Id && x.DirectionId == 1 }).sort(arrivalTimeSort).take(2);
                resultList.push(composeResultItem(stop, nearestArrivals1));
                resultList.push(composeResultItem(stop, nearestArrivals2));
            });

            var nextTransportStore = new Ext.data.Store({
                model: 'NextTransport',
                data: resultList
            });

            list.setStore(nextTransportStore);
            list.refresh();

            Ext.getBody().unmask();
            this.isLoaded = true;
        }
    });
}

function composeResultItem(stop, arrivals) {
    if (arrivals.length == 0)
        return null;

    var currentLocation = getGpsLocation();
    var coord = stop.Point.split(' ');
    var stopLocation = { Lat: parseFloat(coord[0]), Lng: parseFloat(coord[1]) };
    var distance = getDistance(currentLocation.Lat, currentLocation.Lng, stopLocation.Lat, stopLocation.Lng).toFixed(2);
    var resultItem = { StopName: stop.Name, Distance: distance, RouteType: arrivals[0].RouteType, RouteName: arrivals[0].RouteName, Direction: arrivals[0].DirectionText, Time1: arrivals[0].ArrivalTime.format('h:i A') };

    if (arrivals.length > 1)
        resultItem.Time2 = arrivals[1].ArrivalTime.format('h:i A');

    return resultItem;
}

function arrivalTimeSort(a, b) {
    return date_sort_asc(a.ArrivalTime, b.ArrivalTime);
}

function denormalize(stops) {
    var result = new Array();

    $.each(stops, function () {
        var stop = this;

        $.each(this.Arrivals.results, function () {
            var arrival = {
                StopId: stop.Id,
                StopName: stop.Name,
                StopPoint: stop.Point,
                ArrivalTime: parseJSONDateTime(this.ArrivalTime),
                RouteId: this.RouteId,
                RouteName: this.RouteName,
                RouteType: this.RouteType,
                DirectionId: this.DirectionId,
                DirectionText: this.DirectionText
            };
            result.push(arrival);
        });
    });
    return result;
}

